package com.escool.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.escool.model.TbExpedienteHasMateria;
import com.escool.model.TbExpedienteHasRecord;
import com.escool.model.TbRecord;
import com.escool.model.TbSedeHasPersonal;
import com.escool.model.VwUsuarios;
import com.escool.service.ExpedienteService;
import com.escool.service.FindService;
import com.escool.service.ProfAlumnoService;
import com.escool.service.SedeHasPersonalService;
import com.escool.service.TipoRecordService;

@Controller
public class ComunicacionController {
	
	@Autowired
	SedeHasPersonalService sedeHasPersonalService;
	
	@Autowired
	ProfAlumnoService profAlumnoService;
	
	@Autowired
	TipoRecordService tipoRecordService;
	
	@Autowired
	ExpedienteService expedienteService;
	
	@Autowired
	FindService findServie;
	
	@RequestMapping("showComunicacionSedePersonal")
	public String showCatActividad(Map<String, Object> map){
		try{
			User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			map.put("SedePersonal", new TbSedeHasPersonal());
			map.put("TbSedeHasPersonalList", sedeHasPersonalService.listVwPersonalSedeByEmp(user.getUsername()));
			return "showSedePersonal";
		}
		catch(Exception ex){
			ex.printStackTrace();
			return "redirect:/login";
		}
		
			
	}
	
	@RequestMapping("addComunicacionRecord")
	public String showRecord(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("rec") @Valid TbRecord tbRecord,BindingResult result){
		//String id = request.getParameter("id");
		VwUsuarios dto = (VwUsuarios) request.getSession().getAttribute("dto");
		tbRecord.setEmisario(dto.getId().getNombreUsuario());
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy 'a las ' HH:mm:ss");
		String fechaString = sdf.format(date);
		tbRecord.setFecha(fechaString);
		System.out.println(tbRecord.getEmisario());
		profAlumnoService.addRecord(tbRecord);
		System.out.println(tbRecord);
		System.out.println(tbRecord.getIdSeccionHasMateria());
		List xm = expedienteService.listExpMatByMateria(tbRecord.getIdSeccionHasMateria());
		if(!xm.isEmpty()){
			Iterator i = xm.iterator();
			while(i.hasNext()){		    	
			TbExpedienteHasMateria tmp = (TbExpedienteHasMateria) i.next();
			String mat = tmp.getTbSedeSeccionHasMateria().getTbMateria().getNombreMateria();
			TbExpedienteHasRecord xhr = new TbExpedienteHasRecord();
			xhr.setTbExpediente(tmp.getTbExpediente());
			xhr.setTbRecord(tbRecord);
			profAlumnoService.addExpRec(xhr);
			System.out.println(mat + "," + xhr.getTbRecord().getNombreRecord());
			xhr = null;
			}
		}
		//tbRecord.setIdSeccionHasMateria(Integer.valueOf(id));
		//TbExpediente tbExpediente = expedienteService.findById(Integer.valueOf(id));
		map.put("rec", tbRecord);
		map.put("recList", profAlumnoService.listAllRecord());
		map.put("tiporecList", tipoRecordService.listAll());
		return "comunicacion";
	}
	
	@RequestMapping("showComunicacionRecord")
	public String showComunicacionRecord(Map<String, Object> map, HttpServletRequest request){
		try{
		//String id = request.getParameter("id");
		
			User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		map.put("commList", findServie.listVwComunicacionesEntrada(user.getUsername()));
		return "showComunicacionRecord";
		}
		catch(Exception e){
			e.printStackTrace();
			return "redirect:/login";
		}
	}

}
